package com.crayon2f.test;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFTable;
import org.awaitility.Awaitility;
import org.junit.Test;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * Created by feifan.gou@gmail.com on 2024/1/24 16:17.
 */
public class TEST2024_01 {

    @Test
    public void test() {

        String mapping = "PUMP_PUMP03+PUMP_PUMP04+PUMP_PUMP05+PUMP_PUMP06+PUMP_PUMP07+PUMP_PUMP08-PUMP_PUMP14+PUMP_PUMP13-PUMP_PUMP11+PUMP_PUMP10+PUMP_PUMP17";
        String[] split = mapping.split("[+\\-]");
        System.out.println(Arrays.toString(split));
        System.out.println(split.length);
        List<String> list = Lists.newArrayList();
        int index = 0;
        for (int i = 0; i < mapping.length(); i++) {
            char ch = mapping.charAt(i);
            if (Objects.equals(ch, '+') || Objects.equals(ch, '-')) {
                list.add(mapping.substring(index, i));
                list.add(String.valueOf(ch));
                index = i;
            }
            if (i == mapping.length() - 1) {
                list.add(mapping.substring(index));
            }
        }
        list.forEach(System.out::println);
    }

    private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(50, 100, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(50));

    @SneakyThrows
    @Test
    public void testFuture() {

        Future<?> submit = EXECUTOR.submit(() -> {
            System.out.println("run job");
            try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Awaitility.await()
                .atMost(Duration.ofMinutes(1))
                .pollInterval(Duration.ofSeconds(2))
                .until(() -> {
                    System.out.println("000000000000000000");
                    System.out.println(submit.isDone());
                    return submit.isDone();
                });
        System.out.println("done");
    }

    @Test
    public void testPlush100() {

        for (int i = 1000, j = 1; i < 9999 || i - 9999 < 1000; i += 1000, j++) {
            System.out.printf("%d-%d, %d\n", 1000, j, i);
        }
    }

    @Test
    public void removeDeletedData() {

        for (int i = 0; i < 8; i++) {
            Map<String, String> param = ImmutableMap.of("sqlStr", "delete from point_param_instance where del_flag = 1 limit 10000");
            HttpResponse execute = HttpUtil.createPost("http://cim-data-service.fnwintranet.com/data/import/sql/execute")
                    .body(JSONUtil.toJsonStr(param))
                    .header("Content-Type", "application/json")
                    .execute();
            System.out.println(execute.body());
        }
    }

    @Test
    public void test2() {

        Map<String, String> map = new HashMap<>();
        map.put("1", "4");
        map.put("2", "4");
        map.put("3", "4");
        System.out.println(Arrays.toString(map.values().toArray(new String[0])));
    }


    @Test
    public void testWhile() throws InterruptedException {

        while (true) {
            if (isTrue()) {
                System.out.println("=========================");
                break;
            }
            TimeUnit.SECONDS.sleep(5);
        }
    }

    private boolean isTrue() throws InterruptedException {


        System.out.println("000000000000000000000000");
        return false;
    }
}
